<?php

namespace App\Admin\Controllers;

use App\Models\BusinessCategory;
use Encore\Admin\Facades\Admin;
use App\Models\Department;
use Encore\Admin\Controllers\AdminController;
use Encore\Admin\Controllers\ModelForm;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Layout\Column;
use Encore\Admin\Layout\Content;
use Encore\Admin\Layout\Row;
use Encore\Admin\Show;
use Encore\Admin\Tree;
use Encore\Admin\Widgets\Box;
use Illuminate\Support\Facades\DB;
use Illuminate\Http\Request;

class BusinessCategoryController extends AdminController
{
    use ModelForm;
    /**
     * Title for current resource.
     *
     * @var string
     */
    protected $title = '业务分类管理';

    protected $header = '业务分类管理';


    public function index(Content $content)
    {
        return Admin::content(function (Content $content) {
            $content->header($this->header);
            $content->description('业务分类列表');

            $content->row(function (Row $row) {
                $row->column(6, $this->treeView()->render());

                $row->column(6, function (Column $column) {
                    $form = new \Encore\Admin\Widgets\Form();
                    $form->action(admin_base_path('portrait/business-categories'));

                    $form->text('name', '业务分类名称');
                    $form->textarea('description', '业务分类信息描述');
                    $form->number('order', '排序序号')->default(0);
                    $form->select('parent_id','父类名称')->options(BusinessCategory::selectOptions());
                    $form->hidden('_token')->default(csrf_token());

                    $column->append((new Box(trans('admin.new'), $form))->style('success'));
                });
            });
        });
    }

    protected function treeView()
    {
        return BusinessCategory::tree(function (Tree $tree) {
            return $tree;
        });
    }

    public function create(Content $content)
    {
        return Admin::content(function (Content $content) {
            $content->header($this->header);
            $content->description('添加业务分类');
            $content->body($this->form());
        });
    }

    public function edit($id, Content $content)
    {
        return Admin::content(function (Content $content) use ($id) {
            $content->header($this->header);
            $content->description('编辑业务分类');

            $content->body($this->form()->edit($id));
        });
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $grid = new Grid(new BusinessCategory());



        return $grid;
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     * @return Show
     */
    protected function detail($id)
    {
        $show = new Show(BusinessCategory::findOrFail($id));



        return $show;
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        $form = new Form(new BusinessCategory());

        $form->display('id', 'ID');

        $form->text('name', '业务类型名称');
        $form->textarea('description', '业务类型描述');
        $form->number('order', '排序序号');
        $form->select('parent_id', '父类名称')->options(BusinessCategory::selectOptions());

        return $form;
    }

    public function getBusinessCategoryOptions()
    {
        return DB::table('business_categories')->select('id','name as text')->get();
    }

    public function getBusinessCategory(Request $request)
    {
        return DB::table('departments')->where('parent_id','=',2)->get(['id', DB::raw('name as text')]);;
//        $q = $request->q;
//        $myself = DB::table('departments')->where('id', '=', $q)->get();
//        $siblings = DB::table('departments')->where('parent_id','=',$myself->parent_id)->get();
//        $children = DB::table('departments')->where('parent_id','=',$q)->get();

        //return compact($myself, $siblings, $children);
    }
}
